package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.RuleInq;
import com.cfets.annualAssessment.mapper.RuleInqMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by ShenYiChou on 2017/6/6.
 */
@Service
public class RuleInqService {
    @Autowired
    private RuleInqMapper ruleInqMapper;

    public boolean trunRuleInq(){
        Example example=new Example(RuleInq.class);
        return ruleInqMapper.deleteByExample(example)>0;
    }

    public List<RuleInq> getRuleInqPage(RuleInq ruleInq) {
        Page<?> page = PageHelper.startPage(ruleInq.getPage(), ruleInq.getRows());
        List<RuleInq> ruleInqs = this.getRuleInq(ruleInq);
        ruleInq.setTotalCount(page.getTotal());
        return ruleInqs;
    }

    public List<RuleInq> getRuleInq(RuleInq ruleInq) {
        Example example=new Example(RuleInq.class);
        Example.Criteria criteria1 =example.createCriteria();
        Example.Criteria criteria2 =example.createCriteria();
        if(!StringUtils.isEmpty(ruleInq.getRULE_NAME())){
            criteria1.andLike("RULE_NAME","%"+ruleInq.getRULE_NAME()+"%");
            criteria2.andLike("RULE_NAME","%"+ruleInq.getRULE_NAME()+"%");
        }
        if(!StringUtils.isEmpty(ruleInq.getRULE_TAG1())) {//前台只把标签传到TAG1中
            criteria1.andEqualTo("RULE_TAG1",ruleInq.getRULE_TAG1());
            criteria2.andEqualTo("RULE_TAG2",ruleInq.getRULE_TAG1());
        }
        example.or(criteria2);
        return ruleInqMapper.selectByExample(example);
    }

    public boolean newRuleInq(RuleInq ruleInq) {
        return ruleInqMapper.insertSelective(ruleInq)>0;
    }

    public boolean updateRuleInq(RuleInq ruleInq) {
        return ruleInqMapper.updateByPrimaryKey(ruleInq)>0;
    }

    public boolean deleteRuleInqById(int id) {
        return ruleInqMapper.deleteByPrimaryKey(id)>0;
    }
}
